package org.jeecg.modules.lab.syn;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.TokenServess;
import org.jeecg.modules.lab.entrust.entity.LabEntrustSample;
import org.jeecg.modules.lab.entrust.service.ILabEntrustSampleService;
import org.jeecg.modules.lab.entrust.service.ILabWscientificService;
import org.jeecg.modules.lab.notify.service.ILabNotifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.net.MalformedURLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import java.util.concurrent.Callable;

@Slf4j
@Component
public class ExpiredMessageReminder {
    @Autowired
    private ILabNotifyService labNotifyService;
    @Autowired
    private ILabEntrustSampleService labEntrustSampleService;

    /**
     * 样品到期的提醒
     * @param params
     * @return
     * @throws MalformedURLException
     */
    @XxlJob(value = "MessageReminderJob")
    public ReturnT<String> ExpiredMessageReminder(String params) throws MalformedURLException, ParseException {
        QueryWrapper<LabEntrustSample> sampleQueryWrapper = new QueryWrapper<>();
        sampleQueryWrapper.eq("sam_status","备样");
        List<LabEntrustSample> list = labEntrustSampleService.list(sampleQueryWrapper);
        LocalDate date1 = LocalDate.now();
        SimpleDateFormat originalFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
        originalFormat.setTimeZone(TimeZone.getTimeZone("CST"));
        SimpleDateFormat targetFormat = new SimpleDateFormat("yyyy-MM-dd");
        for (LabEntrustSample labEntrustSample : list) {
            if(labEntrustSample.getBackDate()!=null && !"".equals(labEntrustSample.getBackDate()) ){
                String string = labEntrustSample.getBackDate().toString();
                Date originalDate = originalFormat.parse(string);
                String targetDateStr = targetFormat.format(originalDate);
                if (targetDateStr.equals(date1.toString())){
                    //System.out.println("是今天");
                    /**
                     * 线程
                     */
                    Callable<Boolean> c = () -> {
                        try {
                            //调即时通
                            LoginUser loginUser = labNotifyService.byIdLogUser("1685828676077244418");//给龙雨茜发
                            //判断用户是否在数智天研系统中是否有账号，如果有发送消息，否则不发消息
                            String tId = loginUser.getTId();
                            if (!"".equals(tId) && tId != null) {
                                TokenServess tokenServess = new TokenServess();
                                String tokenServe = tokenServess.getTokenServe();
                                tokenServess.getSysmw(labEntrustSample.getSamNum()+"样品将过期", tId, tokenServe);
                            }
                        } catch (Exception e) {
                        }
                        return true;
                    };

                }else {
                    //System.out.println("不是今天");
                }

            }

        }



        return ReturnT.SUCCESS;
    }
}
